import 'package:flutter/material.dart';
import '../tabs2/DetailPage.dart';
import '../../model/LoanApply.dart';
import './TopTabListPage.dart';

class TopTabPage extends StatefulWidget {
  final int tabIndex;

  TopTabPage(this.tabIndex);

  @override
  _TopTabPageState createState() => _TopTabPageState();
}

class _TopTabPageState extends State<TopTabPage>
    with SingleTickerProviderStateMixin {
  var _tabController;
  List<Widget> _tabList;
  List<Widget> _pageList;

  @override
  void initState() {
    super.initState();

    _tabList = <Widget>[
      new Tab(text: "全部"),
      new Tab(text: "待处理"),
      new Tab(text: "待补件"),
      new Tab(text: "审批中"),
      new Tab(text: "待评价"),
    ];

    _pageList = <Widget>[
      // new TopTabListPage(_createDataList(20)),
      // new TopTabListPage(_createDataList(5)),
      // new TopTabListPage(_createDataList(5)),
      // new TopTabListPage(_createDataList(5)),
      // new TopTabListPage(_createDataList(5)),
      new TopTabListPage(10),
      new TopTabListPage(5),
      new TopTabListPage(0),
      new TopTabListPage(2),
      new TopTabListPage(3),
    ];

    // _pageList = <Widget>[
    //   new DetailPage(null, "开发中，敬请期待..."),
    //   new DetailPage(null, "开发中，敬请期待..."),
    //   new DetailPage(null, "开发中，敬请期待..."),
    //   new DetailPage(null, "开发中，敬请期待..."),
    //   new DetailPage(null, "开发中，敬请期待..."),
    // ];

    _tabController = new TabController(
      vsync: this, //动画效果的异步处理，默认格式，背下来即可
      length: _tabList.length, //需要控制的Tab页数量
      initialIndex: widget.tabIndex, // 初始tab位置
    );
  }

  //当整个页面dispose时，记得把控制器也dispose掉，释放内存
  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text('车贷列表'),
        centerTitle: true,
        // bottom: new AppBar(
        //   title: new Material(
        //     color: Colors.blue,
        //     child: new TabBar(
        //       controller: _tabController,
        //       tabs: _tabList,
        //     ),
        //   ),
        // ),
        bottom: new TabBar(
          controller: _tabController,
          tabs: _tabList,
        ),
      ),
      body: new TabBarView(
        controller: _tabController,
        children: _pageList,
      ),
      endDrawer: new Drawer(
        child: new ListView(
          children: <Widget>[
            new Center(
              child: new Container(
                child: new Text("消息提醒",
                    style: new TextStyle(
                        fontSize: 20.0, fontWeight: FontWeight.bold)),
              ),
            ),
            _createCard(context, "消息标题", "消息内容1, 消息内容2消息内容3, 消息内容4消息内容5, 消息内容",
                "2018-07-30 11:22:33", true),
            _createCard(context, "消息标题", "消息内容3, 消息内容4", "2018-07-30 11:22:33", true),
            _createCard(context, "消息标题", "消息内容5, 消息内容", "2018-07-30 11:22:33", false),
            _createCard(context, 
                "消息标题",
                "消息内容1, 消息内容2消息内容3, 消息内容4消息内容5, 消息内容, 消息内容6消息内容7, 消息内容8消息内容9, 消息内容",
                "2018-07-30 11:22:33",
                false),
          ],
        ),
      ),
    );
  }

  Widget _createCard(BuildContext context, String title, String message, String date, bool unread) {
    return new Card(
      margin: EdgeInsets.all(5.0),
      child: new Container(
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.all(5.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            new Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Container(
                  padding: EdgeInsets.all(5.0),
                  child: new Text(
                    title,
                    textAlign: TextAlign.left,
                  ),
                ),
              ],
            ),
            _messageItem(context, title, message, unread),
            new Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                new Container(
                  padding: EdgeInsets.all(5.0),
                  child: new Text(
                    date,
                    textAlign: TextAlign.right,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _messageItem(BuildContext context, String title, String message, bool unread) {
    return unread
        ? new ListTile(
            title: new Text(
              message,
              maxLines: 3,
              softWrap: true,
              overflow: TextOverflow.ellipsis,
            ),
            onTap: () {
              Navigator.of(context).push(new MaterialPageRoute(
                      builder: (BuildContext context) => new DetailPage(title, message)));
            },
            trailing: new Icon(
              Icons.brightness_1,
              size: 12.0,
              color: Colors.blueAccent,
            ),
          )
        : new ListTile(
            title: new Text(
              message,
              maxLines: 3,
              softWrap: true,
              overflow: TextOverflow.ellipsis,
            ),
            onTap: () {
              Navigator.of(context).push(new MaterialPageRoute(
                      builder: (BuildContext context) => new DetailPage(title, message)));
            },
          );
  }

  List<LoanApply> _createDataList(int count) {
    List<LoanApply> dataList;
    for (int i = 1; i <= count; i++) {
      dataList.add(new LoanApply(
        id: i,
        userId: 1,
        applyCode: ("2018000" + i.toString()),
        carType: "BMW 5X",
        carPrice: "180,000.00",
        status: 1,
        loanPayment: "54,000.00",
        loanRepayment: "5,416.67",
        loanDuration: "24月",
        applyTime: "2018-08-05 12:34:56",
      ));
    }
    return dataList;
  }
}
